import React, {Component} from 'react'
import ReactDOM from 'react-dom'
import { Tabs } from 'antd';
import ListProductMainPage from './List';
import ListInfo from './Info';
import Actions from './ActionStore/Actions';
import store from './ActionStore/Stores';

const TabPane = Tabs.TabPane;

export default class ListProductPage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            opType: '新建',
            refresh: false,
            activeKey: '1',
            rowObj: null,
            matchedRows: [] //table列表需要选中的行
        };
        // this.unsubscribe = store.listen( (type, data)=>{
        //     console.log("store.listen   type====", type);
        //     console.log("store.listen   data====", JSON.stringify(data));
        //     this.setState(data);
        // });
        this.unsubscribe = Actions.rowDoubleClick.listen( (type, rowObj) => {
            //alert('rowOb1111111: '+ JSON.stringify(rowObj, null, 4));
            if(rowObj != null){
                this.setState({
                    opType: type,
                    activeKey: '2',
                    rowObj: Object.assign(rowObj[0].data,{rowIndex : rowObj[0].rowIndex}),
                    matchedRows: rowObj[0].rowIndex,
                });
            }else{
                this.setState({
                    opType: type,
                    activeKey: '2'
                });
            }
        });

        this.unsubscribe = Actions.createClick.listen( (type, rowObj) => {
            //alert('rowObj: '+ JSON.stringify(rowObj, null, 4));
            this.setState({
                opType: '编辑',
                refresh: true,
                activeKey: '2',
                rowObj: rowObj,
                //matchedRows: [rowObj.rowIndex ? rowObj.rowIndex : 0],
                matchedRows: 0,
            });
        });
    }

    componentWillUnmount() {
        console.log("组件销毁");
        this.unsubscribe();
    }

    componentDidMount() {
        //Actions.refreshClick('3', values);
    }

    onChangeTabs =(key) => {
        this.setState({
            activeKey: key,
            refresh: false

        });
    }

    render() {
        let rowObj = {};
        let isAdd = true;

        if(this.state.opType != '新建'){
            isAdd = false;
            if(this.state.rowObj != null){
                //alert("this.state.rowObj: " + JSON.stringify(this.state.rowObj, null, 4));
                rowObj = this.state.rowObj;
            }
        }

        return (

            <Tabs onChange={this.onChangeTabs} activeKey={this.state.activeKey}>
                <TabPane tab="产品基础信息" key="1">
                    <ListProductMainPage refresh={this.state.refresh} matchedRows={this.state.matchedRows}/>
                </TabPane>
                <TabPane ref="infoTabPane" tab="明细" key="2" >
                    <ListInfo rowObj={rowObj} isAdd ={isAdd}/>
                </TabPane>
            </Tabs>
        );
    }
}
